Share via


Task 2: Add the User Ranking to the Application

Download sample

In this task, you add a PolicyActivity activity and configure a RuleSet to implement the user ranking portion of the application.

Note

Although you are encouraged to follow the exercises in a linear manner, it is not required. You can start on this exercise by opening the sample project and proceeding to the steps in the following section.

Add Required Workflow Member Variables

In the Workflow1 class, add a member variable to hold the computed ranking.

To add the ranking workflow member

  1. Open Workflow1 in code view (right-click Workflow1.cs or Workflow1.vb in Solution Explorer and choose View Code).

  2. Add the following member declaration to the Workflow1 class.

    public string Ranking;
    

Add the Policy Activity

A PolicyActivity activity is used to execute the rules contained in a RuleSet. In this tutorial, these rules are used to determine the player's ranking.

To add the Policy activity

  1. Switch to the workflow design view (right-click Workflow1.cs or Workflow1.vb in Solution Explorer and choose View Designer).

  2. Drag a Policy activity from the Toolbox and drop it so that it is the last activity in the workflow.

Create the RuleSet

Once the PolicyActivity activity is added, the RuleSet is created.

To configure the RuleSet

  1. Right-click the Policy activity and choose Properties from the context menu.

  2. Select the RuleSetReference property in the Properties window and click the ellipsis button that is displayed to the right of the property.

  3. Click New.

The dialog that is displayed is the Rule Set Editor. This dialog is used to create and edit the rules in a RuleSet. Three rules are required to implement the ranking algorithm for this application.

To add the rules

  1. Click Add Rule.

  2. Enter the following expression into the Condition text box.

    this.Turns <= 10
    
  3. Enter the following expression into the Then Actions text box.

    this.Ranking = "Expert"
    
  4. Click Add Rule.

  5. Enter the following expression into the Condition box.

    this.Turns > 10 && this.Turns <= 20
    
  6. Enter the following expression into the Then Actions text box.

    this.Ranking = "OK"
    
  7. Click Add Rule.

  8. Enter the following expression into the Condition text box.

    this.Turns > 20
    
  9. Enter the following expression into the Then Actions text box.

    this.Ranking = "Needs Work"
    

When the RuleSet executes the value of the workflow's Ranking member is set to the proper value based on the number of turns taken by the user to determine the random number.

To save the rules

  1. Click OK to close the Rule Set Editor dialog box.

  2. Ensure that the newly-created RuleSet is selected in the Name list, and click OK.

Display the Ranking

Once the ranking is calculated, it is displayed to the user using a CodeActivity activity.

To display the ranking

  1. Drag a Code activity from the Toolbox and drop it so that it is the last activity in the workflow.

  2. Double-click the Code activity and enter the following code into the generated handler.

    Console.WriteLine("Your ranking: {0}", Ranking);
    

Build and Run the Application

Build and run the application and see how you rank.

To build and run the application

  1. Press Ctrl+F5 to build and run the application.

In Exercise 2: Use Rules Chaining with the PolicyActivity Activity you add a second PolicyActivity activity and use rules to implement a binary search algorithm to automate the process of determining the random number.

See Also

Concepts

Using the PolicyActivity Activity
Using RuleSets in Workflows

Copyright © 2007 by Microsoft Corporation. All rights reserved.
Last Published: 2010-03-04